const app = getApp();
var Mcaptcha = require('../../utils/mcaptcha');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Uname: '',
    Upwd: '',
    Tel: '',
    LY: '小程序',
    txt: '',
    ckmmt: true,
    isShow: false,
    ckmm: 'icon-yanjing',
    inputYzm: '',
    inputPyzm: '',
    iscode: '',
    disabled: true,
    num: '',
    codename: '获取验证码'
  },
  inputUserName: function (e) {
    this.setData({
      Uname: e.detail.value
    })
  },
  inputTel: function (e) {
    this.setData({
      Tel: e.detail.value
    })
  },
  inputPass: function (e) {
    this.setData({
      Upwd: e.detail.value
    })
  },
  inputYzm: function (e) {
    this.setData({
      inputYzm: e.detail.value
    })
  },
  inputPyzm: function (e) {
    this.setData({
      inputPyzm: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  getRanNum: function () {
    var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
    var pwd = '';
    for (var i = 0; i < 4; i++) {
      if (Math.random() < 48) {
        pwd += chars.charAt(Math.random() * 48 - 1);
      }
    }
    return pwd;
  },

  /**sn.toUpperCase()
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.changePyzm();
  },

  changePyzm: function () {
    var that = this;
    var num = that.getRanNum();
    // console.log(num)
    this.setData({
      num: num
    })
    new Mcaptcha({
      el: 'canvas',
      width: 80, //对图形的宽高进行控制
      height: 30,
      code: num
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  loginckmm: function () {
    if (this.data.ckmmt) {
      this.setData({
        ckmmt: false,
        ckmm: 'icon-yanjing1'
      })
    } else {
      this.setData({
        ckmmt: true,
        ckmm: 'icon-yanjing'
      })
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  logingetyzm: function () {
    this.getCode();
  },
  logingetyzmN: function () {
    // body...
  },
  getCode: function () {
    var that = this;
    var tel = this.data.Tel;

    var myreg = /^(14[0-9]|16[0-9]|19[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
    if (!myreg.test(tel)) {
      app.tishi('请输入正确的手机号');
      return false;
    } else if (this.data.inputPyzm.toUpperCase() != this.data.num.toUpperCase()) {
      app.tishi('请输入正确的图形验证码');
      return false;
    } else {
      var api_input = {
        tel: this.data.Tel
      }
      app.getDaoPost('api/FeijiuApp/Feijiu_GetTelYzm_Reg', api_input, function (r) {
        if (r.code == 200) {
          app.tishi(r.data.Mess);
          that.setData({
            iscode: app.aesDec(r.data.Yzm),
            disabled: false
          })
          var num = 61;
          var timer = setInterval(function () {
            num--;
            if (num <= 0) {
              clearInterval(timer);
              that.setData({
                codename: '重新发送',
                disabled: true,
              })
            } else {
              that.setData({
                codename: num + "s后重新获取"
              })
            }
          }, 1000)
        } else {
          app.tishi(r.message)
        }
      })
    }
  },
  loginBtn: function () {
    var that = this;
    var uPattern = /^[a-zA-Z0-9_-]{5,20}$/;
    var upwdgz = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,14}$/;
    var uTel = /^(14[0-9]|16[0-9]|19[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
    if (!uPattern.test(this.data.Uname)) {
      app.tishi('请输入正确的用户名以字母开头5-20个字符');
      return false
    } else if (!uTel.test(this.data.Tel)) {
      app.tishi('请输入正确的手机号');
      return false
    } else if (!upwdgz.test(this.data.Upwd)) {
      app.tishi('请输入6-14个字符的密码');
      return false
    } else if (this.data.inputPyzm.toUpperCase() != this.data.num.toUpperCase()) {
      app.tishi('请输入正确的图形验证码');
      return false;
    } else if (this.data.inputYzm != this.data.iscode || this.data.inputYzm.length < 6) {
      app.tishi('请输入正确手机验证码');
      return false;
    } else {
      var url = 'api/FeijiuApp/GetUserByUname?UName=' + this.data.Uname + '&strRSA=';
      app.getDaoGet(url, function (r) {
        if (r.code == 200) {
          if (r.data == 0) {
            var api_input = {
              Uname: that.data.Uname,
              Upwd: that.data.Upwd,
              Mobile: that.data.Tel,
              strRSA: app.data.strRSA
            }

            app.getDaoPost('api/FeijiuApp/Feijiu_PostReg', api_input, r => {
              if (r.code == 200) {
                app.tishi(r.data.Mess);
                var pages = getCurrentPages();
                var prevPage = pages[pages.length - 2];
                prevPage.setData({
                  Uname: r.data.UName
                })
                wx.navigateBack();
              } else {
                app.tishi('注册失败,请重试或者联系客服人员');
                return false
              }
            })


            return
            app.getDaoPostOld('api/FeijiuApp/PostReg', api_input, function (r) {
              if (r.code == 200) {
                app.tishi(r.data.Mess);
                var pages = getCurrentPages();
                var prevPage = pages[pages.length - 2];
                prevPage.setData({
                  Uname: r.data.UName
                })
                wx.navigateBack();
              } else {
                app.tishi('注册失败,请重试或者联系客服人员');
                return false
              }

            })
          } else {
            app.tishi(that.data.Uname + ' 此用户名已被使用,请换一个吧');
            return false
          }
        } else {
          app.tishi('注册失败,请重试或者联系客服人员');
          return false
        }
      })
    }




  },

})